
const util = require('../../utils/util.js');
const api = require('../../config/api.js');

Page({
  data: {
    width:"",
    height:"",
    e: undefined,
    s_height: "",
    longitude:"",
    latitude:"",
    tip:'展开更多机构∧',
    markers: [],
    circles:[],
    mechanismList:[],
    controls: [{
      id: 1,
      iconPath: '/resources/location.png',
      position: {
        left: 0,
        top: 300 - 50,
        width: 50,
        height: 50
      },
      clickable: true
    }],
    mapCtx:''
  },
  regionchange(e) {
    var that = this
    if (e.type == "begin"){
      console.log(e.type)
      that.setData({
        width: "100%",
        height: "90vh",
        s_height: "10vh",
        class: "foot-box"
      });
    }
    setTimeout(function () {
      if (e.type == "end") {
        var mechanismLists = wx.getStorageSync('mechanismList');
        if (mechanismLists.length == 0) {
          that.setData({
            width: "100%",
            height: "80vh",
            tip:'五公里范围内没有机构',
            s_height: "2vh",
            class: "foot-box"
          });
        }else{
          that.setData({
            width: "100%",
            height: "50vh",
            s_height: "50vh",
            class: "foot-box2"
          });
        }
     
      }
    }, 1500)
    
  },
  change_adviser(){

    var mechanismLists = wx.getStorageSync('mechanismList');
    if (this.data.height =="0vh"){
      this.setData({
        width: "100%",
        height: "50vh",
        s_height: "50vh",
        class: "foot-box2",
        mechanismList: mechanismLists
      });
    } else {
      this.setData({
        width: "100%",
        height: "0vh",
        s_height: "100vh",
        class: "foot-box3",
        mechanismList: mechanismLists
      });
    }
  },
  user_loc(){
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        console.log(res.latitude)
        console.log(res.longitude)
        console.log(res.speed)
        console.log(res.accuracy)
        const circles = [{
          longitude: longitude,
          latitude: latitude,
          fillColor: '#0000000c',
          color: '#00000000',
          radius: 5000
        }]
        that.setData({
          width: "100%",
          height: "50vh",
          circles: circles,
          latitude: latitude,
          longitude: longitude,
          class: "foot-box2"
        });
      }
    })

  },

  markertap(e) {
    // console.log(e.markerId)
   var list = []
    var mechanismLists = wx.getStorageSync('mechanismList');
    for (var i = 0; i < mechanismLists.length; i++) {
      if (mechanismLists[i].id == e.markerId){
        list.push(mechanismLists[i])
      }
    }
    this.setData({
      mechanismList: list
    });
    


  },
  controltap(e) {
    console.log(e.controlId)
  },
  onReady: function (options) {
 
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
    var that = this;
    // 使用 wx.createMapContext 获取 map 上下文
    this.mapCtx = wx.createMapContext('map')

    // this.getCenterLocation();
    if (e == undefined) {
      const longitude = wx.getStorageSync("longitude")
      const latitude = wx.getStorageSync("latitude")
      if (longitude != "") {

        that.getMechanism(longitude, latitude)
        const circles = [{
          longitude: longitude,
          latitude: latitude,
          fillColor: '#0000000c',
          color: '#00000000',
          radius: 5000
        }]
        that.setData({
          width: "100%",
          height: "50vh",
          circles: circles,
          latitude: latitude,
          longitude: longitude,
          class: "foot-box2"
        });

      } else {
        wx.getLocation({
          type: 'wgs84',
          success(res) {
            const latitude = res.latitude
            const longitude = res.longitude
            const speed = res.speed
            const accuracy = res.accuracy
            console.log(res.latitude)
            console.log(res.longitude)
            console.log(res.speed)
            console.log(res.accuracy)
            wx.setStorageSync('longitude', longitude);
            wx.setStorageSync('latitude', latitude);
            that.getMechanism(longitude, latitude)
            const circles = [{
              longitude: longitude,
              latitude: latitude,
              fillColor: '#0000000c',
              color: '#00000000',
              radius: 5000
            }]
            that.setData({
              width: "100%",
              height: "50vh",
              circles: circles,
              latitude: latitude,
              longitude: longitude,
              class: "foot-box2"
            });
          }
        })
      }
    } else {
      wx.getLocation({
        type: 'wgs84',
        success(res) {
          const latitude = res.latitude
          const longitude = res.longitude
          const speed = res.speed
          const accuracy = res.accuracy
          console.log(res.latitude)
          console.log(res.longitude)
          console.log(res.speed)
          console.log(res.accuracy)
          wx.setStorageSync('longitude', longitude);
          wx.setStorageSync('latitude', latitude);
          that.getMechanism(longitude, latitude)
          const circles = [{
            longitude: longitude,
            latitude: latitude,
            fillColor: '#0000000c',
            color: '#00000000',
            radius: 5000
          }]
          that.setData({
            width: "100%",
            height: "50vh",
            circles: circles,
            latitude: latitude,
            longitude: longitude,
            class: "foot-box2"
          });
        }
      })
    }

  },


  onLoad(e) {
   this.onShow(e)
  },
  getCenterLocation() {
    this.mapCtx.getCenterLocation({
      success(res) {
        console.log(res.longitude)
        console.log(res.latitude)
      }
    })
  },
  getMechanism(longitude, latitude) {
    let that = this;
    const markers =[]
    util.request(api.GetMechanism, { longitude: longitude, latitude: latitude } ).then(function (res) {
      if (res.errno === 0) {
        const list = res.data.list

        wx.setStorageSync('mechanismList', list);

      for (var i = 0; i< list.length;i++){
        const marker = {}
        // marker.iconPath = 'https://ailianshangquan.oss-cn-beijing.aliyuncs.com/location_06.png',
          marker.id = list[i].id,
          marker.latitude = list[i].latitude,
          marker.longitude = list[i].longitude,
          // marker.width =  22,
          // marker.height = 31,
          marker.callout = {
            content: list[i].name,
            fontSize: 14,
            color: '#ffffff',
            bgColor: '#000000',
            padding: 8,
            borderRadius: 4,
            boxShadow: '4px 8px 16px 0 rgba(0)'
        }
          marker.height = 41
          markers.push(marker)
      }
        that.setData({
          markers:markers,
          mechanismList: list
        })
      }
    });
  },
})